﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.IRepository;
using Microsoft.EntityFrameworkCore;

namespace MDT.LifeSewagePlatform.Repository
{
    public class FileRepository: HandleBaseRepository<FileEntity>,IFileRepository
    {
        public FileRepository()
        {

        }

        public IQueryable<FileView> GetFiles(Expression<Func<FileView,bool>> expression)
        {
            var query = from file in GetQuery().Where(d => d.IsDelete == false)
                        join company in _dbContext.Company.AsNoTracking() on file.CompanyID equals company.ID
                        join provience in _dbContext.Regions.AsNoTracking() on company.ProvinceID equals provience.ID
                        join city in _dbContext.Regions.AsNoTracking() on file.CityID equals city.ID
                        join user in _dbContext.Users.AsNoTracking() on file.UserID equals user.ID
                        select new FileView()
                        {
                            FileName = file.FileName,
                            ID = file.ID,
                            CityID = city.ID,
                            CityName = city.DisplayName,
                            ProvinceID = provience.ID,
                            ProvinceName = provience.DisplayName,
                            FileSize = file.FileSize,
                            UpTime = file.CreationTime,
                            UserID = user.ID,
                            UserName = user.UserName,
                            CompanyID = company.ID,
                            CompanyName = company.CompanyName
                        };

            return query.Where(expression);
        }
    }
}
